/*
 * Copyright (c) 2020-2022 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <stdio.h>
#include <unistd.h>
#include "ohos_init.h"
#include "cmsis_os2.h"
#include "hal_bsp_sht20.h"

osSemaphoreId_t envDataSemaphore;

void EnvDataSyncTask(void)
{
    while (1)
    {
        // 每3秒释放两次信号量，确保两个接收任务都能执行
        osSemaphoreRelease(envDataSemaphore);
        osSemaphoreRelease(envDataSemaphore);
        printf("\nEnvironment Data Sync Task: Semaphore released\n");
        osDelay(300); // 3秒延时
    }
}

void EnvDataCollectorTask(void)
{
    float currentTemp = 0, currentHumid = 0;
    printf("Environment Sensor Initialized\n");
    SHT20_Initialize(); // 初始化环境传感器
  
    while (1)
    {
        // 等待数据同步信号
        osSemaphoreAcquire(envDataSemaphore, osWaitForever);
        SHT20_GetMeasurements(&currentTemp, &currentHumid);
        printf("Temperature = %.2f °C | Humidity = %.2f %% \n", currentTemp, currentHumid);
        printf("Environment Data Collector: Semaphore acquired\n");
        osDelay(1); // 10ms延时
    }
}

void EnvDataMonitorTask(void)
{
    while (1)
    {
        // 等待数据同步信号
        osSemaphoreAcquire(envDataSemaphore, osWaitForever);
        printf("Environment Data Monitor: Semaphore acquired\n");
        osDelay(1); // 10ms延时
    }
}

static void InitEnvironmentMonitorSystem(void)
{
    osThreadAttr_t taskConfig;
    taskConfig.attr_bits = 0U;
    taskConfig.cb_mem = NULL;
    taskConfig.cb_size = 0U;
    taskConfig.stack_mem = NULL;
    taskConfig.stack_size = 1024 * 4;

    // 创建数据同步任务
    taskConfig.name = "EnvDataSyncTask";
    taskConfig.priority = 25;
    if (osThreadNew((osThreadFunc_t)EnvDataSyncTask, NULL, &taskConfig) == NULL)
    {
        printf("Failed to initialize Environment Data Sync Task!\n");
    }

    // 创建数据采集任务
    taskConfig.name = "EnvDataCollector";
    taskConfig.priority = 25;
    if (osThreadNew((osThreadFunc_t)EnvDataCollectorTask, NULL, &taskConfig) == NULL)
    {
        printf("Failed to initialize Environment Data Collector!\n");
    }

    // 创建数据监控任务
    taskConfig.name = "EnvDataMonitor";
    taskConfig.priority = 25;
    if (osThreadNew((osThreadFunc_t)EnvDataMonitorTask, NULL, &taskConfig) == NULL)
    {
        printf("Failed to initialize Environment Data Monitor!\n");
    }

    // 初始化信号量 (初始值0，最大值4)
    envDataSemaphore = osSemaphoreNew(4, 0, NULL);
    if (envDataSemaphore == NULL)
    {
        printf("Failed to create Environment Data Semaphore!\n");
    }
}

APP_FEATURE_INIT(InitEnvironmentMonitorSystem);

